package com.hu.zhcc.shiro.controller.Appointment;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.Appointment.AReservation;
import com.hu.zhcc.shiro.entity.Appointment.ReservationSetmeal;
import com.hu.zhcc.shiro.entity.Appointment.TSetmeal;
import com.hu.zhcc.shiro.service.Appointment.ReserService;
import com.hu.zhcc.shiro.service.Appointment.ReservationSetmealService;
import com.hu.zhcc.shiro.service.Appointment.TSetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/****
 * 预约列表Controller
 */
@RestController
public class ReserController {

    @Autowired
    private ReserService reserService;

    @Autowired
    private TSetmealService setmealService;

    @Autowired
    private ReservationSetmealService reservationSetmealService;

    /***
     * 分页及条件查询
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping("reserAll")
    private Page<AReservation> reserAll(String offset, String limit, String value) {
        Map map = new HashMap();
        map.put("offset", offset);
        map.put("limit", limit);
        return reserService.selectAReserAll(map, value);
    }

    /****
     * 添加
     * @param reservation
     * @return
     */
    @PostMapping("addReservation{mid}")
    private int addReservation(@RequestBody AReservation reservation, @PathVariable("mid") int mid) {
        reservation.setTionApptime(reservation.getTionApptime().substring(0, 10));
        reservation.setTionBirth(reservation.getTionBirth().substring(0, 10));
        reserService.insertSelective(reservation);
        String r = reservation.getTionApptime().replace("-", "");
        reservation.setTionNumber(r + reservation.getId());

        ReservationSetmeal reservationSetmeal = new ReservationSetmeal(1, reservation.getId(), mid);
        reservationSetmealService.addReservationSetmeal(reservationSetmeal);
        return reserService.updateByPrimaryKeySelective(reservation);
    }

    /****
     * 套餐分页及添加查询
     * @param offset
     * @param limit
     * @param value
     * @return
     */
    @GetMapping("selectMealAll")
    private Page<TSetmeal> selectMealAll(String offset, String limit, String value) {
        Map map = new HashMap();
        map.put("offset", offset);
        map.put("limit", limit);
        return setmealService.selectTSetmealAll(map, value);
    }

    /****
     * 取消预约的删除列表
     * @param id
     * @return
     */
    @PostMapping("cancelReser{id}")
    private int cancelReser(@PathVariable int id) {
//        return reserService.updateStateByid(id);
        return reserService.deleteByPrimaryKey(id);
    }

    /****
     * 预约
     * @param id
     * @return
     */
    @PostMapping("confirmReser{id}")
    private int confirmReser(@PathVariable int id) {
        return reserService.updateConfirmByid(id);
    }
}
